package guiLayer;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import modelLayer.Forest;
import modelLayer.Owner;
import modelLayer.Tree;

public class Updater extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTabbedPane tabbedPane;
	private JPanel contentPane;
	private JPanel reader;
	
	/**
	 * Create the frame.
	 */
	public Updater(ArrayList<Object> obj, JPanel reader) {
		this.reader = reader;
		draw();
		buildPanels(obj);
	}
	
	public void draw(){
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 690, 425);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
	}
	
	public void buildPanels(ArrayList<Object> obj){
		for(Object o : obj){
			if(o instanceof Owner){
				String title = "Update " + ((Owner)o).getName() + "(Owner)";
				JPanel panel = new CreaterOwner((Owner)o, this);
				tabbedPane.addTab(title, null, panel, null);
			}else if(o instanceof Forest){
				String title = "Update " + ((Forest)o).getName() + "(Forest)";
				JPanel panel = new CreaterForest((Forest)o, this);
				tabbedPane.addTab(title, null, panel, null);
			}else if(o instanceof Tree){
				String title = "Update " + ((Tree)o).getSort() + "(Tree)";
				JPanel panel = new CreaterTree((Tree)o, this);
				tabbedPane.addTab(title, null, panel, null);
			}
		}
	}
	
	public void closeMe(JPanel panel){
		tabbedPane.remove(panel);
		
		if(tabbedPane.getTabCount() < 1){
			if(reader instanceof Reader){
				((Reader)reader).updateData();
			}else if(reader instanceof ReadTree){
				((ReadTree)reader).updateData();
			}
			this.dispose();
		}
	}
	
}
